package com.evil.common.es.dto.application.order.data;

import cn.hutool.core.bean.BeanUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.elasticsearch.core.Nullable;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 控件值 ES实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@Accessors(chain = true)
public class ESTableRowDTO {

    public static final String COL_ORDER_DATA_ID = "order_data_id";
    public static final String COL_CONTROL_ID = "control_id";
    public static final String COL_CONTROL_CODE = "control_code";
    public static final String COL_CONTROL_TITLE = "control_title";
    public static final String COL_PARENT_CONTROL_ID = "parent_control_id";
    public static final String COL_VALUES = "values";
    public static final String COL_VALUES_NESTED = "values_nested";
    /**
     * 数据id
     */
    @Nullable
    @Field(name = COL_ORDER_DATA_ID, type = FieldType.Long)
    private Long orderDataId;
    /**
     * 控件id
     */
    @Nullable
    @Field(name = COL_CONTROL_ID, type = FieldType.Long)
    private Long controlId;
    /**
     * 控件code
     */
    @Nullable
    @MultiField(mainField = @Field(name = COL_CONTROL_CODE, type = FieldType.Text, fielddata = true),
            otherFields = {
                    @InnerField(suffix = "keyword", type = FieldType.Keyword, ignoreAbove = 256)
            }
    )
    private String controlCode;
    /**
     * 控件标题
     */
    @Nullable
    @MultiField(mainField = @Field(name = COL_CONTROL_TITLE, type = FieldType.Text, fielddata = true),
            otherFields = {
                    @InnerField(suffix = "keyword", type = FieldType.Keyword, ignoreAbove = 256)
            }
    )
    private String controlTitle;
    /**
     * 上级控件id
     */
    @Nullable
    @Field(name = COL_PARENT_CONTROL_ID, type = FieldType.Long)
    private Long parentControlId;
    /**
     * 控件值
     */
    @Valid
    @Field(name = COL_VALUES, type = FieldType.Object)
    private List<ESControlDataValueDTO> values;
    /**
     * 控件值
     */
    @Valid
    @Field(name = COL_VALUES_NESTED, type = FieldType.Nested)
    private List<ESControlDataValueDTO> valuesNested;

    public ESTableRowDTO() {
        this.values = new ArrayList<>();
        this.valuesNested = new ArrayList<>();
    }

    public ESTableRowDTO(ESControlDataDTO esControlDataDTO) {
        BeanUtil.copyProperties(esControlDataDTO, this);
        this.values = esControlDataDTO.getValues().stream().map(e -> BeanUtil.copyProperties(e, ESControlDataValueDTO.class)).collect(Collectors.toList());
        this.valuesNested = values;
    }
}
